package fr.upmc.cie.data;

import java.util.ArrayList;

import android.content.Context;

import fr.upmc.cie.data.exception.ChannelNotExistException;
import fr.upmc.cie.data.exception.ChannelTitleDuplicateException;
import fr.upmc.cie.data.interfaces.IChannelDataSource;
import fr.upmc.cie.ext.Utils;

public class ChannelManager {
	
	private IChannelDataSource dataSource;

	protected ChannelManager(IChannelDataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	/**
	 * Renvoie tous les channels
	 * @return
	 */
	public ArrayList<ChannelItem> getAllChannels(){
		return dataSource.getAllChannel();
	}
	
	/**
	 * Renvoie le nom de tous les channels
	 * @return
	 */
	public ArrayList<String> getAllChannelsName(){
		ArrayList<String> names = new ArrayList<String>();
		for (ChannelItem ci : dataSource.getAllChannel()){
			names.add(ci.getTitle());
		}
		return names;
	}
	
	/**
	 * Inserer un channel
	 * @param ci
	 * @param context
	 * @return
	 */
	public boolean insertChannel(ChannelItem ci, Context context){
		try {
			this.dataSource.insert(ci);
			return true;
		} catch (ChannelTitleDuplicateException e) {
			Utils.getAlertBox(context, "Channel title already exist, please select another alias.");
			return false;
		}
	}
	
	/**
	 * Supprimer un channel
	 * @param ci
	 * @param context
	 * @return
	 */
	public boolean deleteChannel(ChannelItem ci, Context context){
		try {
			this.dataSource.delete(ci);
			return true;
		} catch (ChannelNotExistException e) {
			Utils.getAlertBox(context,  "Chennel doesn't exist.");
			return false;
		}
		
	}
	
	/**
	 * Mettre a jour un channel
	 * @param ci
	 * @param context
	 * @return
	 */
	public boolean updateChannel(ChannelItem ci, Context context){
		try {
			this.dataSource.update(ci);
			return true;
		} catch (ChannelTitleDuplicateException e) {
			Utils.getAlertBox(context, "Channel title already exist, please select another alias.");
			return false;
		} catch (ChannelNotExistException e) {
			Utils.getAlertBox(context,  "Chennel doesn't exist.");
			return false;
		}
	}
	
	/**
	 * Obtenir un channel a partir de son nom
	 * @param name
	 * @param context
	 * @return
	 */
	public ChannelItem getChannelByName(String name, Context context){
		try {
			return this.dataSource.getChannelByName(name);
		} catch (ChannelNotExistException e) {
			Utils.getAlertBox(context,  "Chennel doesn't exist.");
			return null;
		}
	}
	
	/**
	 * Obtenir un channel a partir de son id
	 * @param id
	 * @param context
	 * @return
	 */
	public ChannelItem getChannelById(Long id, Context context){
		try {
			return this.dataSource.getChannelById(id);
		} catch (ChannelNotExistException e) {
			Utils.getAlertBox(context,  "Chennel doesn't exist.");
			return null;
		}
	}
	
	/**
	 * Parametrer le categorie de la channel dans la base
	 * @param c
	 * @param ci
	 */
	public void setChannelCategorie(ChannelItem c, CategorieItem ci){
		ci.getChannels().add(c);
		dataSource.setChannelCategorie(c, ci);
	}
	
	public long getChannelCategorieId(ChannelItem c){
		return dataSource.getChannelCategorieId(c);
	}

}
